﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using Apps.Models.PUB;
using System;

namespace Apps.BLL.PUB
{
    public partial class PUB_MATERIELBLL
    {

        public override List<PUB_MATERIELModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            IQueryable<PUB_MATERIEL> queryData = null;
            string pid = parentId.ToString();
            if (pid != "0")
            {
                queryData = m_Rep.GetList(a => a.PUB_MATERIEL_TYPE.MATERIEL_TYPE_ID == pid);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
                            a => (
                                    a.TEMPLATE_ID.Contains(queryStr)
                                   || a.KKSCODE.Contains(queryStr)
                                   || a.REMARKS.Contains(queryStr)
                                   || a.MATERIEL_TYPE_ID.Contains(queryStr)
                                   || a.MATERIEL_NAME.Contains(queryStr)
                                   || a.MATERIEL_ID.Contains(queryStr)
                                 ) && a.PUB_MATERIEL_TYPE.IS_COAL == true
                            );
            }
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public List<PUB_MATERIELModel> GetListBySupplierId(ref GridPager pager, string SupplierId)
        {
            IQueryable<PUB_MATERIEL> queryData = null;
            string pid = SupplierId.ToString();
            queryData = m_Rep.GetListBySupplierId(SupplierId);
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public List<PUB_MATERIELModel> GetListNotINSupplierId(ref GridPager pager, string queryStr,string SupplierId)
        {
            IQueryable<PUB_MATERIEL> queryData = null;
            if (string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetListNotINSupplierId(SupplierId);
            }
            else
            {
                queryData = m_Rep.GetListNotINSupplierId(SupplierId).Where(p=>p.MATERIEL_NAME.Contains(queryStr)|| p.REMARKS.Contains(queryStr));
            }

                pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
    }
}

